package com.carrental.api.pms.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.api.pms.mapper.PmsStoreMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.api.common.exception.Assert;
import com.carrental.common.exception.OperationException;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.common.utils.CommonQueryWrapper;
import com.carrental.api.pms.domain.PmsStore;
import com.carrental.api.pms.domain.bo.PmsStoreBo;
import com.carrental.api.pms.domain.vo.PmsStoreVo;
import com.carrental.api.pms.service.IPmsStoreService;
import com.carrental.common.helper.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LiangYi
 * @since 2022/3/23 00:58
 */
@Service
@Slf4j
public class PmsStoreServiceImpl extends ServiceImpl<PmsStoreMapper,PmsStore> implements IPmsStoreService {
    /**
     * 获取门店分页
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public IPage<PmsStoreVo> pageList(CommonQueryParam commonQueryParam) {
        //支持多个参数查询，如门店地址
        return this.baseMapper.pageList(
            new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
            new CommonQueryWrapper<PmsStore>(commonQueryParam).lambda()
                .eq(PmsStore::getIsDeleted, false)
        );
    }

    /**
     * 为门店分配管理员
     *
     * @param storeId 门店标识
     * @param adminId 管理员标识
     * @return x
     */
    @Override
    public Boolean storeManagerAdd(Long storeId, Long adminId) {
        PmsStore store=this.getOne(
            new QueryWrapper<PmsStore>().lambda()
                .eq(PmsStore::getStoreId,storeId)
                .eq(PmsStore::getIsDeleted,false)
        );
        Assert.notNull(store,"无此门店信息！");
        store.setManagerId(adminId);
        if(!this.updateById(store)){
            throw new OperationException("分配门店管理员失败！");
        }
        return true;
    }

    /**
     * 添加门店
     *
     * @param storePost 门店参数
     * @return 是否成功
     */
    @Override
    public Boolean create(PmsStoreBo storePost) {
        PmsStore store=new PmsStore();
        BeanUtils.copyProperties(storePost,store);
        if (!this.save(store)) {
            throw new OperationException("添加门店失败！");
        }
        return true;
    }

    /**
     * 逻辑删除
     *
     * @param id id
     * @return 是否成功
     */
    @Override
    public Boolean delete(Long id) {
        //先查询，再更新
        PmsStore store = this.getById(id);
        Assert.notNull(store, "删除失败，无此门店！");
        store.setIsDeleted(true);
        if (!this.updateById(store)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 修改门店信息
     * @param id id
     * @param storePost 门店参数
     * @return 是否成功
     */
    @Override
    public Boolean update(Long id,PmsStoreBo storePost) {
        //先查询，再更新
        PmsStore store = this.getById(id);
        Assert.notNull(store, "修改失败，无此门店！");
        BeanUtils.copyProperties(storePost, store,"storeId");
        if (!this.updateById(store)) {
            throw new OperationException("添加门店信息失败！");
        }
        return true;
    }

    /**
     * 通过点击门店，获取门店id，查看门店详细信息
     * @param id 门店参数
     * @return 门店信息
     */
    @Override
    public PmsStore detail(Long id) {
        PmsStore store=this.getById(id);
        Assert.notNull(store,"查询失败，无此门店");
        return store;
    }

    /**
     * 获取门店列表
     *
     * @param commonQueryParam 查询参数，城市、门店名称等等
     * @return 列表
     */
    @Override
    public List<PmsStore> list(CommonQueryParam commonQueryParam) {
        //使用MyBatisPlus进行列表查询
        return this.list(
            new CommonQueryWrapper<PmsStore>(commonQueryParam).lambda()
                .eq(PmsStore::getIsDeleted, false)
        );
    }
}
